package oldtest;

import gui.*;
import boardgame.*;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.*;
import java.util.Iterator;
import java.util.Vector;
import javax.swing.JPanel;
import javax.swing.KeyStroke;
import javax.swing.ButtonGroup;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JRadioButtonMenuItem;

public class TicTacToeGUITest extends BoardGUI {	
		
	public Vector<GButton> buttonGrid = new Vector<GButton>();
	
	public TicTacToeGUITest(){
		super();
	}
	
	public TicTacToeGUITest(int r, int c, int d){
		super(r,c,d);
	}
	
	protected void setModel(Board model){
		this.model = model;
	}
	
	public void clear(int win){
		Iterator<GButton> i = buttonGrid.iterator();
		while(i.hasNext()){
			i.next().setEnabled(false);
		}		
		if (win < 0)
			statusBar.setText("Tie Game!");
		else
			statusBar.setText("Game Over, Player "+win+" wins!");
	}
	
	public void reset(){
		Iterator<GButton> i = buttonGrid.iterator();
		while(i.hasNext()){
			GButton temp = i.next();
			temp.setEnabled(true);
			temp.setIcon(null);
		}		
		statusBar.setText("New Game!");		
	}
	
	public JMenuBar createJMenuBar() {
		JMenuBar mainMenuBar;
		JMenu menu1, menu2;
		JMenuItem newGameMenuItem, exitMenuItem;
		JRadioButtonMenuItem rbMenuItem;		
		mainMenuBar = new JMenuBar();
		menu1 = new JMenu("Menu");
		menu1.setMnemonic(KeyEvent.VK_M);		
		mainMenuBar.add(menu1);
		// New Game
		newGameMenuItem = new JMenuItem("New Game!", KeyEvent.VK_N);
		newGameMenuItem.setAccelerator(KeyStroke.getKeyStroke(
				KeyEvent.VK_N, ActionEvent.CTRL_MASK));
		newGameMenuItem.addActionListener(new NewGameListener(this, model));
		menu1.add(newGameMenuItem);				
		menu1.addSeparator();
		// Set Number of Players
		ButtonGroup itemGroup = new ButtonGroup();
		rbMenuItem = new JRadioButtonMenuItem("1 Player");		
		rbMenuItem.setMnemonic(KeyEvent.VK_1);
		itemGroup.add(rbMenuItem);
		rbMenuItem.addActionListener(new SwitchAIListener(this, model));
		menu1.add(rbMenuItem);
		rbMenuItem = new JRadioButtonMenuItem("2 Players");
		rbMenuItem.setSelected(true);
		rbMenuItem.setMnemonic(KeyEvent.VK_2);
		itemGroup.add(rbMenuItem);
		rbMenuItem.addActionListener(new SwitchAIListener(this, model));
		menu1.add(rbMenuItem);
		menu1.addSeparator();
		// Exit
		exitMenuItem = new JMenuItem("Exit", KeyEvent.VK_X);		
		exitMenuItem.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent arg0) {
				System.exit(0);
			}});
		menu1.add(exitMenuItem);
		menu1.addSeparator();		
		// Build second menu in the menu bar.
		menu2 = new JMenu("Help");
		menu2.setMnemonic(KeyEvent.VK_H);
		mainMenuBar.add(menu2);
		return mainMenuBar;
	}
	
	public void setupWindow() {
		super.setDefaultWindow();
		mainFrame.setSize(new Dimension(300, 350));
		mainFrame.setResizable(false);
	}
	
	public JPanel createGrid() {
		JPanel grid = new JPanel(); 
    	grid.setLayout(new GridBagLayout());
        GridBagConstraints c = new GridBagConstraints();
        c.fill = GridBagConstraints.HORIZONTAL;
        c.gridx = 0;
        c.gridy = 0;
        for (int i = 0; i < rows; i++){
        	c.gridx = 0;
        	for (int j = 0; j < cols; j++){
        		int[] id = {i, j, 1};
        		GButton button = new GButton(id);
        		button.addActionListener(new GButtonListener(this, model));
        		grid.add(button,c);
        		buttonGrid.add(button);
        		c.gridx++;
        	}
        	c.gridy++;        	
        }    
        if(rows >= 10 && cols >= 10)
        	grid.setSize(500, 500);
        else if (rows >= 10)
        	grid.setSize(500, 50*cols);
        else if (cols >= 10)
        	grid.setSize(50*rows, cols);
        else
        	grid.setSize(50*rows, 50*cols);
        return grid;
	}	
	
	public void update(Object object){
		if (((TicTacToeTest)this.model).previousPlayer == 1)
			((GButton)object).setIcon(new XIcon());
		else
			((GButton)object).setIcon(new OIcon());
		((GButton)object).setEnabled(false);
	}
	
	public void updateStatus(String status){
		statusBar.setText(status);
	}
}
